package ar.edu.unq.svnpermission.provider;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import ar.edu.unq.svnpermission.domain.Group;
import ar.edu.unq.svnpermission.domain.User;

/**
 * @author nicolas
 *
 */
public class GroupsDataProvider extends SortableDataProvider<Group> {

	private static final long serialVersionUID = 1L;
	private List<Group> groups = new ArrayList<Group>();
	
	public GroupsDataProvider()
	{
		
	}
	
	public GroupsDataProvider(List<Group> groups)
	{
		this.fixList(groups);
	}
	
	/**
	 * Se utiliza para remover de la lista las instancias invalidas. Perdon, no me quedo otra!
	 */
	private void fixList(List<Group> groups)
	{
		
		Iterator<Group> it = groups.iterator();
		
		//FIXME
		while(it.hasNext())
		{
			Object item=it.next(); 
			
			if(item.getClass()==Group.class)
			{
				Group group = (Group)item;			
				this.getGroups().add((Group)item);
			}
		}
	}	


	public Iterator<? extends Group> iterator(int first, int count) {
		return this.getGroups().iterator();
	}

	public int size() {
		return this.getGroups().size();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public IModel<Group> model(Group object) {
		return new Model(object);
	}

	public List<Group> getGroups() {
		return groups;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}



}
